package com.example.demo.Service;

import com.example.demo.Entity.Medicalrecords;
import com.example.demo.Entity.transfer.MedicalrecordsDTO;
import com.example.demo.Mapper.MedicalrecordsMapper;
import com.example.demo.util.CommonConstants;
import com.example.demo.util.downloadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MedicalrecordsService   {

    @Autowired
    private MedicalrecordsMapper medicalrecordsMapper;




    /**
     * 查病例——手机号
     * */
    public Map<String, Object> QueryMedicalcareByphone( String phone){
        Map<String, Object> map = new HashMap<String, Object>();
        List<Medicalrecords> medicalrecords = medicalrecordsMapper.getMedicalrecordsByPhone(phone);
        if(medicalrecords != null){
            map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
            map.put(CommonConstants.RESP_MESSAGE, "查询成功");
            map.put(CommonConstants.RESULT,medicalrecords);
        }else{
            map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
            map.put(CommonConstants.RESP_MESSAGE, "未获取到该手机号的病例");
        }
        return map;
    }


    /**
     * 查病例——手机号
     * */
    public Map<String, Object> QueryAllMedicalcareByphone(){
        Map<String, Object> map = new HashMap<String, Object>();
        List<Medicalrecords> medicalrecords = medicalrecordsMapper.getMedicalrecords();
        if(medicalrecords != null){
            map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
            map.put(CommonConstants.RESP_MESSAGE, "查询成功");
            map.put(CommonConstants.RESULT,medicalrecords);
        }else{
            map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
            map.put(CommonConstants.RESP_MESSAGE, "未获取到该手机号的病例");
        }
        return map;
    }



    //根据userid号获取病例
	public Map<String, Object> QueryMedicalcareByUserId( int user_id){
		Map<String, Object> map = new HashMap<String, Object>();
		List<Medicalrecords> medicalrecords = medicalrecordsMapper.getMedicalrecordsByUserId(user_id);
		if(medicalrecords != null){
			map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
			map.put(CommonConstants.RESP_MESSAGE, "查询成功");
			map.put(CommonConstants.RESULT,medicalrecords);
		}else{
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "未获取到该用户的病例");
		}
		return map;
	}

	//根据id号获取病例
	public Map<String, Object> QueryMedicalcareById( int id){
		Map<String, Object> map = new HashMap<String, Object>();
        List<Medicalrecords> medicalrecords = medicalrecordsMapper.getMedicalrecordListById(id);
		if(medicalrecords != null){
			map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
			map.put(CommonConstants.RESP_MESSAGE, "查询成功");
			map.put(CommonConstants.RESULT,medicalrecords);
		}else{
			map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
			map.put(CommonConstants.RESP_MESSAGE, "未获取到该用户的病例");
		}
		return map;
	}


    //根据id号删除病例
    public Map<String, Object> DelMedicalcareById( int id){
        Map<String, Object> map = new HashMap<String, Object>();
        int num =  medicalrecordsMapper.deleteMedicalrecordsById(id);
        if(num > 0){
            map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
            map.put(CommonConstants.RESP_MESSAGE, "删除成功");
        }else{
            map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
            map.put(CommonConstants.RESP_MESSAGE, "删除失败");
        }
        return map;
    }



    /**
     * 病例新增
     * */
    @Transactional(rollbackFor = {Exception.class})
    public Map<String, Object> addMedicalcareById(MedicalrecordsDTO medicalrecordsDTO){
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            if(medicalrecordsMapper.addMedicalrecords(medicalrecordsDTO)>0 ){
                map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
                map.put(CommonConstants.RESP_MESSAGE, "添加成功");
            }else{
                map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
                map.put(CommonConstants.RESP_MESSAGE, "添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
            map.put(CommonConstants.RESP_MESSAGE, "添加病例异常");
            map.put(CommonConstants.EXCEPTION_MESSAGE,e.getMessage());
            return map;
        }
        return map;
    }

    /**
     * 病例修改
     * */
    @Transactional(rollbackFor = Exception.class)
    public Map<String,Object>  update(MedicalrecordsDTO medicalrecordsDTO){
        Map<String, Object> map = new HashMap<String, Object>();
        try {
           int num = medicalrecordsMapper.update(medicalrecordsDTO);
           System.err.println(num);

            System.err.println(medicalrecordsDTO.getId());
            if( num > 0 ){
                map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
                map.put(CommonConstants.RESP_MESSAGE, "修改成功");
            }else{
                map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
                map.put(CommonConstants.RESP_MESSAGE, "修改失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
            map.put(CommonConstants.RESP_MESSAGE, "修改病例异常");
            map.put(CommonConstants.EXCEPTION_MESSAGE,e.getMessage());
            return map;
        }
        return map;
    }

    public Map<String,Object> download(int id){
        Map<String, Object> map = new HashMap<String, Object>(5);
        Medicalrecords medicalrecords = medicalrecordsMapper.getMedicalrecordsById(id);
        if(medicalrecords != null){
            try {
                String contTxt = "姓名："+medicalrecords.getName()+"&"+"年龄："+medicalrecords.getAge()+"&"+ "性别："+medicalrecords.getSex()+"&"+"诊断结果："+medicalrecords.getDiagnosis()+"&"+"医生叮嘱："+medicalrecords.getConclusion()+"&"+"======下载时间："+downloadUtil.getDate(1)+"====";
                String path = downloadUtil.download(contTxt,medicalrecords.getName());
                map.put(CommonConstants.RESP_CODE, CommonConstants.SUCCESS);
                map.put(CommonConstants.RESP_MESSAGE, "下载成功");
                map.put(CommonConstants.RESULT, path);
            }catch (Exception e){
				e.printStackTrace();
                map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
                map.put(CommonConstants.RESP_MESSAGE, "下载失败");
                return map;
            }
        }else{
            map.put(CommonConstants.RESP_CODE, CommonConstants.FALIED);
            map.put(CommonConstants.RESP_MESSAGE, "未获取到该用户的病例");
        }

        return map;
    }

}
